<!---
	Author:	Ramey, Christopher
	Title:	APIs for external access to data
--->
<cfcomponent output="false">

	<!--- *** REQUIRED ***
		  Application name, should be unique at SCF.  Available within your application as APPLICATION.ApplicationName.
		  Where practical, make this name the same as your Database and CF Data Source. --->
	<cfset THIS.name = "api">

	<!--- *** OPTIONAL / REQUIRED? ***
		 (REQUIRED only if using SESSION variables; but for discussion--making this and SESSION.myUserInfo REQUIRED would
		  give us a way to track which users are using the app on a given day.)
		  Are you using any SESSION variables in this application?  If so, you need the lines below.  If you are not using
		  SESSIONS, take these out.		
		  Setting sessionManagement to true enables sessions (otherwise SESSION. does not work), while sessionTimeout
		  determines how long SESSION. values will persist, if not cleared.  Default timeout is 9 hours, and maximum
		  is 2 days (both of which are configured in CFAdmin).
	 --->
	<cfset this.sessionManagement = true>
	<cfset this.sessionTimeout = createTimeSpan(0,9,0,0)>

	<!--- onApplicationStart() - Runs when the Application starts up (when the first .cfm page is accessed).  If you
								 include the "resetApp" code in onRequestStart(), you can also run onApplicationStart()
								 by including a URL parameter "resetApp". --->
	<cffunction name="onApplicationStart" returnType="boolean" output="false">

		<cflock name="AppReset" type="exclusive" timeout="10">
	
			<!--- *** REQUIRED - Keep current version here.  Recommend showing this somewhere in your app. --->
			<cfset APPLICATION.PROGRAM_VERSION = "1.0">

			<!--- *** REQUIRED - Create an entry in the ColdFusion application.log file. --->
			<cflog type="Information" text="#THIS.Name# Application Started" />
		
		</cflock>
	
		<cfreturn true>
	</cffunction>


	<!--- onApplicationEnd() - Runs if/when the application times out (see THIS.applicationTimeout above) --->
	<cffunction name="onApplicationEnd" returnType="void" output="false">
		<cfargument name="applicationScope" required="true">
		
		<!--- *** REQUIRED - Create an entry in the ColdFusion application.log file. --->
		<cflog type="Information" text="#THIS.Name# Application Ended" />
		
	</cffunction>

	
	<!--- onSessionStart() - Runs when each session starts. --->
	<cffunction name="onSessionStart" returnType="void" output="false">
		
		<!--- *** RECOMMENDED (for INTRANET Apps Only) - Save Commonly-Needed Info about the current user into
				  SESSION.myUserInfo.  For Internet (Public) apps, this must be taken out. --->
		<cfif !IsDefined("SCF_Employee_GetEmpInfo_FromLogin")>
			<cfinclude template="/code_lib/SCF_Employee.cfm" />
		</cfif>	
		<cfset SESSION.myUserInfo = SCF_Employee_GetEmployee(ListGetAt(CGI.AUTH_USER, 2, '\')) />
		<cfif SESSION.myUserInfo.Disabled GT 0>
			PERMISSION DENIED.
			<cfabort />
		</cfif>
	
	</cffunction>
	
	
	<!--- onRequestStart() - Runs before each .cfm page request is processed. --->
	<cffunction name="onRequestStart" returnType="boolean" output="false">
		<cfargument name="thePage" type="string" required="true">
		
		<!--- *** RECOMMENDED - This is a neat little trick that allows you to specify "resetApp" in your URL, which
								will reload all of the APPLICATION variables. --->
		<cfif IsDefined("URL.resetApp")>
			<cfset OnApplicationStart()>
		</cfif>
		<cfreturn true>
	</cffunction>
	
	<!--- *** REQUIRED ***   Of course, you'll want to comment this out while in development. --->
	<!--- onError() - Runs on any error.  This is a way to customize global error-handling throughout your app, and 
					  overrides the settings in CFAdmin: Settings.  In the future, this will use the SCF Application
					  database. --->
	<cffunction name="onError" returnType="void" output="TRUE">
		<cfargument name="Exception" required="true">
		<cfargument name="EventName" type="string" required="true">
		
		<!--- developerEMail will eventually come from the SCF Application database.  For now, type in your own. --->
		<cfset var developerEMail = "cramey@scf.cc" />
		
		<!--- Create Log Entry --->
		<cflog type="error" text="#UCase(Exception.Type)# ERROR:  #Exception.Message#    LOCATION: #CGI.SERVER_NAME##CGI.SCRIPT_NAME#, Line #Exception.TagContext[1].Line#." />
		
		<!--- Send E-Mail to Developer --->
			<cfmail type="html" from=""" Your Friendly ColdFusion Server"" <noreply@scf.cc>" to="#developerEMail#"  subject="Doh! Error in #APPLICATION.ApplicationName#">

			<cfinclude template="/code_lib/SCF_Standard_Error_EMail.cfm" />
		</cfmail>
		
		<cfinclude template="/code_lib/SCF_Standard_Error_Display.cfm" />
		
		<!--- *** OPTIONAL - If you just want to throw the regular CF Error--for troubleshooting, perhaps--enable the 
							 line below. --->
		<cfthrow object="#ARGUMENTS.Exception#" />
		
	</cffunction>
	
</cfcomponent>